package 线程池.CompletableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/4/24 13:41
 * @description:
 * @version:
 */
public class ShowThread {

    //创建线程池  第一个参数代表核心线程数
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        System.out.println("main....start....");
        //没有返回结果
        /*CompletableFuture.runAsync(() -> {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        },executor);*/

        //有返回结果
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor);
        try {
            Integer num = future.get();
            System.out.println(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("main....end....");
        executor.shutdown();
    }

}

